﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using TPCN.Commons;

namespace TPCN.Admincp
{
    public partial class Doanhthu : System.Web.UI.Page
    {
        private static string HoadonbanID = String.Empty;
        private static string ChitiethoadonbanID = String.Empty;
        private static List<HoaDonBanObj> source;
        private static List<ChiTietHoaDonBanObj> listhang;
        private const string CMD_DETAIL = "cmdViewDetail";
        private static DateTime start;
        private static DateTime end;
        private static string customerId = "-1";
        private static double sum;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridViewPanel.Visible = true;
                DetailGridPanel.Visible = false;
                LoadCustomer();
            }
        }

        private void BindGridView()
        {
            source = new List<HoaDonBanObj>();
            if (customerId == "-1")
                source = HoaDonBanDAL.GetByDate(start, end);
            else
                source = HoaDonBanDAL.GetByDateAndCustomerId(start, end, customerId);
            HBDGridView.DataSource = source;
            HBDGridView.DataBind();
        }

        private void LoadCustomer()
        {
            List<KhachHangObj> mlist = new List<KhachHangObj>();
            mlist = KhachHangDAL.GetByAll();
            cboCustomer.Items.Add(new ListItem("Tất cả", "-1"));
            cboCustomer.Items.Add(new ListItem("Khách lẻ", "0"));
            for (int i = 0; i < mlist.Count; i++)
            {
                cboCustomer.Items.Add(new ListItem(mlist[i].Hovaten, mlist[i].KhachID));
            }
        }

        protected void HDBGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            HBDGridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void HDBGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            HoadonbanID = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case CMD_DETAIL:
                    LoadDetailPanel();
                    DetailGridPanel.Visible = true;
                    GridViewPanel.Visible = false;
                    break;
            }
        }

        protected void LoadDetailPanel()
        {
            listhang = new List<ChiTietHoaDonBanObj>();
            listhang = ChiTietHoaDonBanDAL.GetByHoadonbanID(HoadonbanID);
            DetailGridView.DataSource = listhang;
            DetailGridView.DataBind();
        }

        protected string FormatDatetime(Object o)
        {
            DateTime time = Convert.ToDateTime(o);
            return time.ToString("dd/MM/yyyy");
        }

        protected string GetCustomerNameById(Object id)
        {
            string mID = Convert.ToString(id);
            KhachHangObj customer = KhachHangDAL.GetByID(mID);
            return (String.IsNullOrEmpty(customer.Hovaten)) ? "Khách lẻ" : customer.Hovaten;
        }

        protected string GetDiscountNameByID(Object id)
        {
            string mID = Convert.ToString(id);
            string s = String.Empty;
            if (mID == Const.KEY_DISCOUNT_TYPE_MONEY)
                s = "Tiền mặt";
            else
                s = "Phần trăm";
            return s;
        }

        protected string GetProductNameById(Object id)
        {
            string mID = Convert.ToString(id);
            return HangDAL.GetByID(mID).Tenhang;
        }

        protected string FormatDiscount(Object typeID, Object discount)
        {
            string mID = Convert.ToString(typeID);
            string value = Convert.ToString(discount);
            if (mID == Const.KEY_DISCOUNT_TYPE_MONEY)
                value += " <sup>đ</sup>";
            else
                value += " %";
            return value;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            GridViewPanel.Visible = true;
            DetailGridPanel.Visible = false;
        }

        protected void DetailGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DetailGridView.PageIndex = e.NewPageIndex;
            LoadDetailPanel();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            start = dateStart.SelectedDate.Value;
            end = dateEnd.SelectedDate.Value;
            customerId = cboCustomer.SelectedValue;
            BindGridView();
            GridViewPanel.Visible = true;
            DetailGridPanel.Visible = false;
            sum = 0;
            for (int i = 0; i < source.Count; i++)
            {
                sum += HoaDonBanDAL.GetRealMoneyOfOrder(source[i].HoadonbanID);
            }
            lblMoney.Text = sum.ToString("N0");
        }

        protected string Tongtien(Object id)
        {
            string mID = Convert.ToString(id);
            double mMoney = HoaDonBanDAL.GetRealMoneyOfOrder(mID);
            return mMoney.ToString("N0");
        }
    }
}